﻿
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;

namespace Jst.Infrastructure.DependencyInjection 
{
    public static class ServiceCollectionExtensions
    {
        public static IServiceCollection AddBusinessServices(
            this IServiceCollection services,
            params Assembly[] assemblies)
        {
            foreach (var asm in assemblies)
            {
                foreach (var type in asm.GetTypes())
                {
                    if (type.IsAbstract || !type.IsClass) continue;

                    var interfaces = type.GetInterfaces();
                    if (interfaces.Length == 0) continue;

                    var serviceType = interfaces.First();

                    if (type.Name.EndsWith("Service"))
                        services.AddSingleton(serviceType, type);
                    else if (type.Name.EndsWith("Repository"))
                        services.AddScoped(serviceType, type);
                    else
                        services.AddTransient(serviceType, type);
                }
            }
            return services;
        }
    }
}